<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.Supplier" %>
<%@page import="db.SupplierDAO" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDAO = (PurreqDAO)ctx.getBean("PurreqDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	SupplierDAO supplierDAO = (SupplierDAO)ctx.getBean("SupplierDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	String pid = request.getParameter("pid");
	String[] matid = request.getParameterValues("matid");
	String[] sjnum = request.getParameterValues("sjnum");
	List list =new ArrayList();
	String message = "";
	boolean success = false;
	if(matid.length>0 && pid!=null){//确认有勾选物资
		list = purmatDAO.findByPurreqId(Integer.parseInt(pid));
		if(list!=null && list.size()>0){//至没有勾选的为无效状态
			for(int i=0;i<list.size();i++){
				Purmat purmat = new Purmat();
				purmat = (Purmat)list.get(i);
				purmat.setState(2);//无效状态
				for(int j=0;j<matid.length;j++){//对比没有勾选中的有至为有效状态1.
					int mid = Integer.parseInt(matid[j]);
					int num = Integer.parseInt(sjnum[j]);
					if(mid==purmat.getId()){
						purmat.setState(1);
						purmat.setDecrease(num);
					}
				}
				purmatDAO.attachDirty(purmat);
			}
		}
		
		//更新审批状态
		Purreq purreq = purreqDAO.findById(Integer.parseInt(pid));
		int userid = (Integer)session.getAttribute("id");
		purreq.setCostUserid(userid);
		purreq.setCostDate(new Date());
		//purreq.setCostState(2);
		purreq.setCostState(2);//利旧审核状态为审核通过
		purreq.setState(4);//临时采购订单
		purreqDAO.attachDirty(purreq);
		success = true;
		message = "恭喜您！利旧审核批准成功！";
	}else{
		message = "利旧审核批准失败！";
		success = false;
	}
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/procure/examineCost.vm");
%>
